package cn.it.shop.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.it.shop.util.GlobalConstants;
import cn.it.shop.util.HttpUtils;
import cn.it.shop.util.JsonUtil;

/***
 * 跳转页面用
 * @author fangyangsheng
 *
 */
@Controller
@RequestMapping("/jump")
public class JumpController extends BaseController{
	
	/**
	 * 转发到投诉页面
	 * @param fans
	 * @param request
	 * @return
	 * */
	@RequestMapping("/flow/complaint")
	public String toComplaint(
			HttpServletRequest request){

		return "flow/complaint";//转发到投诉页面
	}
	
	/**
	 * 转发到建议页面
	 * @param fans
	 * @param request
	 * @return
	 * */
	@RequestMapping("/flow/proposal")
	public String toProposal(
			HttpServletRequest request){

		return "flow/proposal";//转发到新建页面
	}
	
	
	/**
	 * 
	 * 转发到大转盘
	 * @param fans
	 * @param request
	 * @return
	 * */
	@RequestMapping("/activity/toTurntable")
	public String toTurntable(
			HttpServletRequest request){

		return "turntable/turntable";//转发到新建页面
	}
	
	
	/**
	 * 授权后重定向的回调链接地址
	 * <p>Title: showpage</p>
	 * <p>Description: </p>
	 * @param page
	 * @return
	 */
	@RequestMapping("/autho/autho")
	public String toAutho(
			HttpServletRequest request){
		
		//第一步 : 获取code
		Map map = HttpUtils.getRequest(request);
		//System.out.println("一开始"+map);
		Object code = map.get("code");//获取code
		//用户同意授权
		if(code == null){
			return "404";
		}
		request.setAttribute("code", map.get("code"));
		//第二步 : 通过code换取网页授权access_token
		//获取url
		String authoAccessTokenUrl = GlobalConstants.getInterfaceUrl("autho_access_token");	
		//设置参数
		Map<String, String> accessTokenParams = new HashMap<String, String>();
		accessTokenParams.put("appid", GlobalConstants.getInterfaceUrl("appid"));
		accessTokenParams.put("secret", GlobalConstants.getInterfaceUrl("AppSecret"));
		accessTokenParams.put("code", code.toString());
		accessTokenParams.put("grant_type","authorization_code");
		
		
		try {
			String result = HttpUtils.sendGet(authoAccessTokenUrl, accessTokenParams);
			Map<String, Object> accesstokenJson = JsonUtil.analyzeJsonMap(result);//结果结转为map
			System.out.println("网页授权access_token  " + accesstokenJson);
			//没有错误信息
			if(accesstokenJson.get("errcode") == null){
				request.setAttribute("access_token", accesstokenJson.get("access_token"));
				request.setAttribute("refresh_token", accesstokenJson.get("refresh_token"));
				request.setAttribute("openid", accesstokenJson.get("openid"));
			
				//第四步：拉取用户信息(需scope为 snsapi_userinfo)
				//获取url
				String authoUserinfo = GlobalConstants.getInterfaceUrl("autho_userinfo");	
				Map<String, String> userInfoParams = new HashMap<String, String>();
				userInfoParams.put("access_token", accesstokenJson.get("access_token").toString());
				userInfoParams.put("openid", accesstokenJson.get("openid").toString());
				userInfoParams.put("lang","zh_CN");
				
				String userInfoResult = HttpUtils.sendGet(authoUserinfo, userInfoParams);
				Map<String, Object> userInfoJson = JsonUtil.analyzeJsonMap(userInfoResult);//结果结转为map
				request.setAttribute("nickname",userInfoJson.get("nickname"));
				request.setAttribute("headimgurl",userInfoJson.get("headimgurl"));
				request.setAttribute("unionid",userInfoJson.get("unionid"));
				
				System.out.println("获取用户信息"+userInfoJson);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "autho/autho";//转发到新建页面
	}
	
	
	
	
}
